package com.cszy.yydqbfq.ui.fragment

import android.content.Intent
import android.os.Bundle
import com.cszy.yydqbfq.R
import com.cszy.yydqbfq.adapter.IndexListAdapter
import com.cszy.yydqbfq.base.BaseVMFragment
import com.cszy.yydqbfq.bean.Song
import com.cszy.yydqbfq.common.PlaylistRepository
import com.cszy.yydqbfq.databinding.FragmentCategoryBinding
import com.cszy.yydqbfq.dialog.BottomPopupWithGeDanRecycler
import com.cszy.yydqbfq.ui.activity.PlayDetailActivity
import com.cszy.yydqbfq.viewmodel.IndexViewModel
import com.hlyj.http.ad.NewGroMoreFeedAdManager
import com.hlyj.http.base.tool.lib_hlyj_base.bean.music.MusicListBean
import com.hnyyac.ad.mds.IAdView
import com.hnyyac.ad.mds.IAdViewListener
import com.hnyyac.ad.mds.manager.AdManager
import com.hnyyac.ad.mds.manager.AdManager.AdLoadListener
import com.hnyyac.ad.mds.utils.BaseAdConstants

class CategoryFragment : BaseVMFragment <IndexViewModel, FragmentCategoryBinding>(),IAdViewListener{

    companion object {
        fun newInstance(category: String): CategoryFragment {
            val fragment = CategoryFragment()
            val bundle = Bundle()
            bundle.putString("category", category)
            fragment.arguments = bundle
            return fragment
        }
    }

    private var category:String?=null
    private var listAdapter: IndexListAdapter? = null
    private var newGromoreFeedAd : NewGroMoreFeedAdManager? =null
    var type_proces: String? = null //功能入口类型

    override fun initView(savedInstanceState: Bundle?) {
        newGromoreFeedAd = NewGroMoreFeedAdManager(requireActivity(), BaseAdConstants.FEEDAD_Id, mBinding?.feedContainer,33)
        newGromoreFeedAd?.loadAd()
    }

    fun showFirstAd() {
        type_proces="第一次插屏"
        AdManager.loadChapin_nocache(
            requireActivity(),
            21,
            this,
            object : AdLoadListener {
                override fun onAdLoadFailed(ad: IAdView?) {
                    newGromoreFeedAd = NewGroMoreFeedAdManager(requireActivity(), BaseAdConstants.FEEDAD_Id, mBinding?.feedContainer,33)
                    newGromoreFeedAd?.loadAd()
                }
            })
    }


    override fun initData() {
        category = arguments?.getString("category")
        listAdapter = IndexListAdapter()
        mBinding.rvSong.layoutManager = androidx.recyclerview.widget.LinearLayoutManager(requireActivity())
        mBinding.rvSong.adapter = listAdapter
        mViewModel.queryMusicByCategory(category!!)


        listAdapter?.setOnItemClickListener { adapter, view, position ->
            // 传递整个列表和当前点击位置
            var songList=mViewModel.convertToSongInfoList(adapter.items)
            val intent = Intent(requireActivity(), PlayDetailActivity::class.java).apply {
                putParcelableArrayListExtra("MUSIC_LIST", ArrayList(songList))
                putExtra("CURRENT_POSITION", position)
            }
            startActivity(intent)
        }

        // 需要传递控件 id加入歌单点击事件
        listAdapter?.addOnItemChildClickListener(R.id.tv_add_gedanlist) { adapter, view, position ->
            // toast("点了添加歌单")
            var song_item=adapter.items.get(position)
            // 将 MusicListBean 转换为 Song
            val song = Song().apply {
                title = song_item.name ?: ""
                artist = song_item.author ?: ""
                cover = song_item.imageUrl ?: ""
                uri=song_item.songUrl
                duration = 0
                songlistbean_id = 0
            }
            val repository = PlaylistRepository()
            var geDanList = repository.getAllPlaylists()
                .filter { it.name != "本地音乐" }  // 过滤条件
            BottomPopupWithGeDanRecycler(requireActivity(), geDanList,song).show(mBinding.root)
        }
    }

    override fun initObserve() {
        //获取音频数据
        mViewModel.queryMusicResult.observe(this){musiclist->
            listAdapter?.submitList(musiclist as List<MusicListBean>?)
        }
    }

    override fun onTrigger(adView: IAdView) {

    }

    override fun onAdShow(adView: IAdView) {

    }

    override fun onAdClick(adView: IAdView) {

    }

    override fun onAdClose(adView: IAdView) {
//        when(type_proces){
//            "第一次插屏"->{
//                newGromoreFeedAd = NewGroMoreFeedAdManager(requireActivity(), BaseAdConstants.FEEDAD_Id, mBinding?.feedContainer,33)
//                newGromoreFeedAd?.loadAd()
//            }
//        }
    }

    override fun onError(msg: String) {

    }

    override fun onLoadError(adView: IAdView) {

    }

    override fun onSuccess(adView: IAdView) {

    }

    override fun onLoad(adView: IAdView) {

    }

    override fun onSkipped() {

    }

    override fun onNoAd() {

    }

    override fun onTimeOut() {

    }


}